var express = require('express');
var router = express.Router();
const uuid = require('node-uuid');
const sql = require('../sql');
const utils = require('../utils');
const Pro = require('../sql/col/Pro');
const Cart = require('../sql/col/Cart');
const Order = require('../sql/col/Order');

router.get('/', function(req,res, next) {
    res.send('order相关接口')
});

/**
 * @api {post} /api/order/getConfirmOrder 获取订单数据
 * @apiName PostOrderGetConfirmOrder
 * @apiGroup order
 * 
 * @apiParam { string } userid 用户编号
 * @apiParam { string } time 下单时间
 * @apiParam { string } token 用户登录令牌
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
        code: '200',
        message: '获取订单数据成功',
        data
    })

 * @apiSampleRequest /api/order/getConfirmOrder
 * @apiVersion 0.0.0
 */
router.post('/getConfirmOrder', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        const { userid, time } = req.body
        sql.find(Order, { userid , time },{_id:0}).then(data => {
            res.send({
                code: '200',
                message: '获取订单数据成功',
                data
            })
        })
    })
})

/**
 * @api {post} /api/order/confirmOrder 提交订单
 * @apiName PostOrderConfirmOrder
 * @apiGroup order
 * 
 * @apiParam { string } userid 用户编号
 * @apiParam { string } token 用户登录令牌
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
        code: '200',
        message: '提交订单成功',
        time
    })
 * @apiSampleRequest /api/order/confirmOrder
 * @apiVersion 0.0.0
 */
router.post('/confirmOrder', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        const { userid } = req.body
        sql.find(Cart, { userid, flag: true}, {_id:0}).then(data => {
            const arr = []
            data.forEach(item => {
                arr.push(sql.find(Pro, {proid: item.proid},{_id:0}))
            })

            Promise.all(arr).then(result => {
                const list = []
                const time = new Date().getTime()

                result.forEach((item, index) => {
                    list.push({
                        orderid: 'order_' + uuid.v1(),
                        proid: data[index].proid,
                        userid,
                        time, // 下单时间
                        status: 0, // 0 待付款 1 待发货 2 待收货 3 待评价 4 已完成 
                        name: '',
                        tel: '',
                        address: '',
                        proname: item[0].proname,
                        proimg: item[0].proimg,
                        price: item[0].price,
                        num: data[index].num
                    })
                })
                // insertMany直接插入数组是符合语法的
                sql.insert(Order, list).then(() => {
                    res.send({
                        code: '200',
                        message: '提交订单成功',
                        time
                    })
                })
            })
        })
    }) 
})

/**
 * @api {post} /api/order/deleteCartData 删除订单相关的购物车
 * @apiName PostOrderDeleteCartData
 * @apiGroup order
 * 
 * @apiParam { string } userid 用户编号
 * @apiParam { array } list 商品id的数组
 * @apiParam { string } token 用户登录令牌
 * 
 * @apiSuccessExample {json} Success-Response:
 *  res.send({
        code: '200',
        message: '删除购物车数据成功'
    })
 * @apiSampleRequest /api/order/deleteCartData
 * @apiVersion 0.0.0
 */
router.post('/deleteCartData', (req, res, next) => {
    utils.validateToken(req, res).then(() => {
        const { userid, list } = req.body
        const arr = []
        list.forEach(item => {
            arr.push(sql.delete(Cart, { userid, proid: item}))
        })
        Promise.all(arr).then(() => {
            res.send({
                code: '200',
                message: '删除购物车数据成功'
            })
        })
    })
})

module.exports = router;